package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.po.ErpMaterial;
import org.example.domain.po.ErpMaterialCurrentStock;
import org.example.service.ErpMaterialCurrentStockService;
import org.example.mapper.ErpMaterialCurrentStockMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
* @author 24059
* @description 针对表【erp_material_current_stock(产品当前库存)】的数据库操作Service实现
* @createDate 2024-11-04 10:02:47
*/
@Service
public class ErpMaterialCurrentStockServiceImpl extends ServiceImpl<ErpMaterialCurrentStockMapper, ErpMaterialCurrentStock>
    implements ErpMaterialCurrentStockService{

    /**
     * 修改仓库库存
     * @param materialId 商品id
     * @param depotId 仓库id
     * @param number 数量
     */
    @Override
    public boolean updateStock(Long materialId, Long depotId, BigDecimal number, Long tenantId) {
        LambdaQueryWrapper<ErpMaterialCurrentStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpMaterialCurrentStock::getMaterialId, materialId)
                .eq(ErpMaterialCurrentStock::getDepotId, depotId);

        ErpMaterialCurrentStock erpMaterialCurrentStock = baseMapper.selectOne(wrapper);
        if (erpMaterialCurrentStock == null){
            // 如果为空，新增
            ErpMaterialCurrentStock stock = new ErpMaterialCurrentStock(materialId, depotId, number, tenantId);
            return save(stock);
        } else {
            // 原库存
            BigDecimal currentNumber = erpMaterialCurrentStock.getCurrentNumber();

            BigDecimal add = currentNumber.add(number);
            return lambdaUpdate().set(ErpMaterialCurrentStock::getCurrentNumber, add)
                    .eq(ErpMaterialCurrentStock::getMaterialId, materialId)
                    .eq(ErpMaterialCurrentStock::getDepotId, depotId).update();
        }
    }
    @Override
    public boolean updateStock2(Long materialId, Long depotId, BigDecimal number, Long tenantId, String type) {
        LambdaQueryWrapper<ErpMaterialCurrentStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpMaterialCurrentStock::getMaterialId, materialId)
                .eq(ErpMaterialCurrentStock::getDepotId, depotId);

        ErpMaterialCurrentStock erpMaterialCurrentStock = baseMapper.selectOne(wrapper);
        if (erpMaterialCurrentStock == null){
            // 如果为空，新增
            ErpMaterialCurrentStock stock = new ErpMaterialCurrentStock(materialId, depotId, number, tenantId);
            return save(stock);
        } else {
            // 原库存
            BigDecimal currentNumber = erpMaterialCurrentStock.getCurrentNumber();
            BigDecimal updatedNumber;

            if ("入库".equals(type)) {
                // 入库时，增加库存
                updatedNumber = currentNumber.add(number);
            } else if ("出库".equals(type)) {
                // 出库时，减少库存
                updatedNumber = currentNumber.subtract(number);
            } else {
                // 不支持的类型
                throw new IllegalArgumentException("不支持的操作类型: " + type);
            }

            return lambdaUpdate().set(ErpMaterialCurrentStock::getCurrentNumber, updatedNumber)
                    .eq(ErpMaterialCurrentStock::getMaterialId, materialId)
                    .eq(ErpMaterialCurrentStock::getDepotId, depotId).update();
        }
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal1 = new BigDecimal("123.45");
        BigDecimal bigDecimal2 = new BigDecimal("-67.89");
        bigDecimal1.add(bigDecimal2);
        System.out.println(bigDecimal1); // 输出结果
    }


        @Override
    public boolean insertMaterial(ErpMaterialCurrentStock  currentStock) {
        // 调用 Mapper 层插入商品
        int result = baseMapper.insertMaterial(currentStock);
        return result > 0;
    }

}




